package com.pmy.service;

import com.pmy.dao.UserMapper;
import com.pmy.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("UserServiceImpl")
public class UserServiceImpl implements UserService{

    @Autowired
    UserMapper userMapper;


    @Override
    public User queryCodePassword(String userCode, String userPassword){

        User loginUser = null;
        Map<String, String> map = new HashMap<String, String>();
        map.put("userCode",userCode);
        map.put("userPassword",userPassword);

        try {
            loginUser = userMapper.queryCodePassword(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return loginUser;
    }

    @Override
    public List<User> queryAllUser(String queryUserRole, String queryUserName, int currentPageNo, int pageSize) {
        List<User> userList = null;
        Map<String, Object> map = new HashMap<String,Object>();
        map.put("roleId",queryUserRole);
        map.put("userName",queryUserName);
        map.put("currentPageNo",currentPageNo);
        map.put("pageSize",pageSize);
        try {
            userList = userMapper.queryAllUser(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userList;
    }

    @Override
    public int getUserCount(String id, String userName) {
        int userCount = 0;
        Map<String, Object> map = new HashMap<String,Object>();
        map.put("id",id);
        map.put("userName",userName);
        try{
            userCount = userMapper.getUserCount(map);
        } catch (Exception e) {
            e.printStackTrace();
        }


        return userCount;
    }

    @Override
    public User getUserById(int id) {
        return userMapper.getUserById(id);
    }

    @Override
    public int addUser(User user) {
        return userMapper.addUser(user);
    }

    @Override
    public int deleteUserById(int userId) {
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("userId",userId);
        return userMapper.deleteUserById(map);
    }


    @Override
    public int updateUserById(User user) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("userId",user.getId());
        map.put("userCode",user.getUserCode());
        map.put("userName",user.getUserName());
        map.put("gender",user.getGender());
        map.put("birthday",user.getBirthday());
        map.put("phone",user.getPhone());
        map.put("address",user.getAddress());
        map.put("userRole",user.getUserRole());

        return userMapper.updateUserById(map);
    }

}
